package cn.itsy.web.controller;

import cn.itsy.service.log.LogService;
import cn.itsy.utils.Syslog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: MisterGay
 * @date: 2019-06-11 9:32
 */
@Controller
@RequestMapping("sysLog")
public class SysLogController {
    @Resource(name = "logServiceimpl")
    private LogService logService;

    @RequestMapping(value = "querySysLogAll", method = RequestMethod.GET)
    @ResponseBody
    public List<Syslog> querySysLogAll(@RequestParam Map<String, Object> map) {
        String sql = "select * from syslog";
        List<Syslog> syslogsSize = this.logService.querySyslogAll(sql);

        //第几页
        Integer is = Integer.valueOf(map.get("i").toString());
        //每页显示多少行
        Integer isum = 10;
        //总页数
        Integer count = syslogsSize.size() % isum > 0 ? syslogsSize.size() / isum + 1 : syslogsSize.size() / isum;

        //判断点击的是哪一个按钮
        if (map.get("state").equals("首页")) {
            is = 1;
        }
        if (map.get("state").equals("上一页")) {
            is = is <= 1 ? 1 : is - 1;
        }
        if (map.get("state").equals("下一页")) {
            is = is >= count ? count : is + 1;
        }
        if (map.get("state").equals("尾页")) {
            is = count;
        }
        if (map.get("state").equals("跳转")) {
            if (map.get("reverse") != null && !map.get("reverse").equals("")) {
                System.out.println(map.get("reverse") + "=-=-=-=-=-=-=-=-=-==-=-=");
                Integer a = new Integer((String) map.get("reverse"));
                if (a > count) {
                    is = count;
                }
                if (a < count) {
                    is = Integer.valueOf(map.get("reverse").toString());
                }
            }
        }
        int isx = (is - 1) * isum;
        sql += " limit " + isx + "," + isum + " ";

        List<Syslog> syslogs = this.logService.querySyslogAll(sql);
        System.out.println(syslogs + "=======================================================================");
        return syslogs;
    }
}
